package com.spring.sunflower.login;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.spring.sunflower.MainActivity;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.common.WebViewActivity;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.userinfo.GenderSelectActivity;
import com.spring.sunflower.util.AppUtils;


/**
 * Created by Kevin on 2021/5/21<br/>
 * <p>
 * Describe:<br/>
 */
public class LoginByPhoneActivity extends BaseActivity<LoginPresenter> implements View.OnClickListener, LoginContract.View {
    private EditText etPhoneNum, etPhoneCode;
    private TextView tvGetCode, tvLogin;
    private CountDownTimer countDownTimer;
    private boolean isOnTick = false;
    private LinearLayout llPhoneContainer;
    private String mType = "-1";
    // 禁止EditText输入空格
    private InputFilter filter = (source, start, end, dest, dstart, dend) -> {
        if (source.equals(" ")) return "";
        else return null;
    };

    @Override
    public void initView() {
        tvTitle.setText("手机号登录");
        etPhoneNum = findViewById(R.id.etPhoneNum);
        etPhoneCode = findViewById(R.id.etPhoneCode);
        tvGetCode = findViewById(R.id.tvGetCode);
        tvLogin = findViewById(R.id.tvLogin);
        llPhoneContainer = findViewById(R.id.ll_phone_container);
//        InputFilter[] filter = {new NameLengthFilter(11, this)};
//        etPhoneNum.setFilters(filter);
        etPhoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                printD("s=" + s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                printD("sss=" + s.toString());
                if (s.toString().length() == 11) {
                    tvGetCode.setBackgroundResource(R.drawable.ic_btn_bg_click_enable_corner_5);
//                    tvGetCode.setClickable(true);
                } else {
                    tvGetCode.setBackgroundResource(R.drawable.ic_btn_bg_click_disable_corner_5);
//                    tvGetCode.setClickable(false);
                }
            }
        });
        etPhoneCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 4) {
                    tvLogin.setBackgroundResource(R.drawable.ic_btn_bg_click_enable_corner_5);
//                    tvLogin.setBackgroundColor(ContextCompat.getColor(LoginByPhoneActivity.this, R.color.colorPrimary));
                } else {
                    tvLogin.setBackgroundResource(R.drawable.ic_btn_bg_click_disable_corner_5);
                }
            }
        });
        tvGetCode.setOnClickListener(this);
        tvLogin.setOnClickListener(this);
        llPhoneContainer.setOnClickListener(this);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_login_by_phone;
    }

    @Override
    public LoginPresenter setPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public void onClick(View v) {
        if (isFastClick()) return;
        switch (v.getId()) {
            case R.id.tvGetCode:
                if (isOnTick) return;
                String mPhoneNum = etPhoneNum.getText().toString().trim();
                if (TextUtils.isEmpty(mPhoneNum)) {
                    showToast("手机号不能为空");
                    return;
                }
//                if (!checkBox.isChecked()) {
//                    showToast("请勾选同意协议");
//                    return;
//                }
                if (mPhoneNum.length() != 11 || !AppUtils.isValidPhoneNum(mPhoneNum)) {
                    showToast("请输入正确手机号");
                } else {
//                    String ao = getField(Constant.KEY_ACCOUNT_OFF, "");
//                    if (!TextUtils.isEmpty(ao)) {
//                        String[] split = ao.split(",");
//                        List<String> list = Arrays.asList(split);
//                        String time = String.valueOf(System.currentTimeMillis());
//                        for (int i = 0; i < split.length; i++) {
//                            String[] str = split[i].split("&");
//                            if (mPhoneNum.equals(str[1])) {
//                                String fitTimeSpan = TimeUtils.getFitTimeSpan(time, str[2], 5);
//                                showToast(fitTimeSpan);
//                            }
//                        }
//                    }
                    mPresenter.requestAuthCode(mPhoneNum, "0");
                    startCountDown();
                }
                break;
            case R.id.tvLogin:
                String code = etPhoneCode.getText().toString().trim();
                String phoneNum = etPhoneNum.getText().toString().trim();
                if (TextUtils.isEmpty(phoneNum)) {
                    showToast("手机号不能为空");
                    return;
                }
                if (phoneNum.length() != 11 || !AppUtils.isValidPhoneNum(phoneNum)) {
                    showToast("请输入正确手机号");
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    showToast("验证码不能为空");
                    return;
                }
                if (code.length() != 4) {
                    showToast("验证码位数不够");
                    return;
                }
                mPresenter.loginByAuthCode(phoneNum, code);
                break;
            case R.id.ll_phone_container:
                etPhoneNum.requestFocus();
                break;
        }
    }

    @Override
    public void requestFinish() {
        super.requestFinish();
//        Intent intent = new Intent(this, LoginByAuthCodeActivity.class);
//        intent.putExtra("phoneNum", etPhoneNum.getText().toString().trim());
//        startActivity(intent);
    }

    @Override
    public void requestFinishWithSth(String type) {
        super.requestFinishWithSth(type);
        if ("0".equals(type)) {
            startActivity(new Intent(this, GenderSelectActivity.class));
        } else {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }

    public void goAgreement() {
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.putExtra(Constant.KEY_WEB_VIEW_TITLE, "用户协议");
        intent.putExtra(Constant.KEY_WEB_VIEW_URL, Constant.REGISTER_AGREEMENT);
        startActivity(intent);
    }

    public void goPrivateAgreement() {
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.putExtra(Constant.KEY_WEB_VIEW_TITLE, "隐私协议");
        intent.putExtra(Constant.KEY_WEB_VIEW_URL, Constant.PRIVATE_AGREEMENT);
        startActivity(intent);
    }

    public void startCountDown() {
        isOnTick = true;
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvGetCode.setText("倒计时" + millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                isOnTick = false;
                tvGetCode.setText("重新获取");
            }
        };
        countDownTimer.start();
    }
}
